// JavaScript Document
//$(function(){
	var vm = new Vue({
		el:'#vue-el',
		data:{
			rooms: [],
			roomIndex: 0,
			sbIndex: 0,
			current:1,
			isScolling:false,
			itemIndex:0,
			products: [],
			brands: null,
			brandId: null,
			recBrands: null, // 拎包入住推荐的品牌
			recBrandId: null,
			prices: [
			         {id:1, min:0, max:2500},
			         {id:2, min:2501, max:5000},
			         {id:3, min:5001, max:7500},
			         {id:4, min:7501, max:10000},
			         {id:5, min:10000, max:null}
			],
			priceId: null,
			page: 1,
			pageSize: 10,
			loading: false,
			noProduct: false,
			searchTerm: '',
			spaces: null,
			spaceObjectId: 1,
			orderBy: '',
			// 户型
			houseTypes: null,
			houseTypeId: null,
			houseStyles: null,
			houseStyleId: null,
			showMain: false, // 显示创建方案主体
			showList: false, // 显示自定义下拉
			customCategories: // 自定义分类 
				[
				 	"灯具", "花艺", "饰品", "挂画", "布艺家纺", "地毯", "摆件"
				 ]
		},
		computed:{
			
			total:function(){
				var count = 0;
				for(var i =0 ;i< this.rooms.length;i++){
					for(var j= 0; j<this.rooms[i].items.length;j++){
						var item = this.rooms[i].items[j];
						if(!!item.count){
							count += item.count;
						}
					}	
				}
				return count;
			},
			totalPrice:function(){
				var total = 0;
				for(var i =0 ;i< this.rooms.length;i++){
					for(var j= 0; j<this.rooms[i].items.length;j++){
						var item = this.rooms[i].items[j];
						if((!!item.price) && (!!item.count)){
							total += item.price*item.count;
						}
					}	
				}
				return total;
			}	
		},
		methods:{
			total2:function(index){ // 单个room的
				var count = 0;
				for(var i =0 ;i< this.rooms.length;i++){
					if (i != index) {
						continue;
					}
					for(var j= 0; j<this.rooms[i].items.length;j++){
						var item = this.rooms[i].items[j];
						if(!!item.count){
							count += item.count;
						}
					}	
				}
				return count;
			},
			totalPrice2:function(index){ // 单个room的
				var total = 0;
				for(var i =0 ;i< this.rooms.length;i++){
					if (i != index) {
						continue;
					}
					for(var j= 0; j<this.rooms[i].items.length;j++){
						var item = this.rooms[i].items[j];
						if((!!item.price) && (!!item.count)){
							total += item.price*item.count;
						}
					}	
				}
				return total;
			},
			resetSb: function(roomIndex, sbIndex) { // reset space object
				var r = this.rooms[roomIndex];
				var sb = r.items[sbIndex];
				sb.img = sb.defaultImg;
				sb.count = 0;
				sb.price = 0;
				sb.specificationId = null;
				sb.name = sb.defaultName;
			},
			changeSb: function(roomIndex, sbIndex) { // 点某一空间
				var r = this.rooms[roomIndex];
				var sb = r.items[sbIndex];
				vm.searchTerm = sb.name;
				this.roomIndex = roomIndex;
				this.sbIndex = sbIndex;
				this.updateProducts();
			},
			setShowCustom: function(roomIndex) { // 显示自定义
				console.log('setShowCustom, roomIndex: ' + roomIndex)
				this.rooms[roomIndex].showCustom = true;
			},
			addCustom: function(roomIndex, customName) { // 增加自定义的
				var item = {
						name: customName,
						defaultName: customName,
						price: 0,
						count: 0,
						img: "/img/sf_ico.png",
						defaultImg: "/img/sf_ico.png"
				};
				this.rooms[roomIndex].items.push(item);
				this.rooms[roomIndex].showCustom = false;
			},
			addSpace: function(r) {
				this.rooms.push(r);
			},
			next: function() {
				if (this.houseTypeId == null) {
					error("请选择户型");
					return;
				}
				if (this.houseStyleId == null) {
					error("请选择风格");
					return;
				}
				if (this.recBrandId == null) {
					error("请选择品牌");
					return;
				}
				this.showMain = true;
			},
			updateBrand: function(id) {
				this.brandId = id;
			},
			addItem:function(item){
				var id =0;
				for(var i=0;i<this.rooms.length;i++){
					if(this.rooms[i].id > id){
						id = this.rooms[i].id;	
					}	
				}
				id +=1;
				this.rooms.push({id:id,name:item,show:true,items:[{type:'床',img:'img/sf_ico.png',count:0,id:'',name:'',price:0},{type:'沙发',img:'img/sf_ico.png',count:0,id:'',name:'',price:0},{type:'自定义',img:'img/zdy_ico.png',count:0,id:'',name:'',price:0}]});
			},
			changeIndex:function(id){
				this.current = id;
			},
			changeIndex:function(index){
				var room = this.rooms[index];
				this.current =  room.id;
				for(var i=0;i<this.rooms.length;i++){
					var _room = this.rooms[i];
					if(i<index && index!=0){
						_room.show = false;
					}else{
						_room.show = true;
					}
					this.rooms.splice(i,1,_room)
				}
			},
			scrollTo:function(index){
				this.roomIndex = index;
				this.changeIndex(index);
				this.$nextTick(function(){
					this.isScolling = true;
					var children = $('.left_pz dl>div').eq(index);
					var top = children.offset().top+10;
					var that = this;
					$('html,body').animate({scrollTop:top+'px'},function(){
						that.isScolling = false;
					});
					//$(document).scrollTop(top);
				});
				
			},
			onChangeIndex:function(index,rid){
				this.roomIndex = index;
				this.current = rid;
				this.itemIndex=index;
			},
			changeItem:function(item){
				for(var i =0 ;i< this.rooms.length;i++){
		//			if(this.rooms[i].id == this.current){
	//					var type = this.rooms[i].items[this.itemIndex].type;
//						this.rooms[i].items.splice(this.itemIndex,1,{type:type,img:item.img,count:1,id2:item.id,name:item.name,price:item.price, specificationId: item.specificationId});
					var r = this.rooms[i];
					for (var j = 0; j < r.items.length; j++) {
						var it = r.items[j];
						if (it.id == vm.spaceObjectId) {
							it.img = item.img;
							it.specificationId = item.specificationId;
							it.name = item.name;
							it.price = item.price;
							it.count = 1;
						}
					}
				}
			},
			updateProducts: function() {
				var r;
				loading()
				vm.loading = true;
				vm.noProduct = false;
				vm.products = [];
				var minPrice, maxPrice;
				for (var i = 0; i < this.prices.length; i++) {
					if (this.prices[i].id == this.priceId) {
						minPrice = this.prices[i].min;
						maxPrice = this.prices[i].max;
					}
				}
				$.ajax({
					url: "/design/getProducts",
					data: {
						brandId: vm.recBrandId || vm.brandId,
						startPrice: minPrice,
						endPrice: maxPrice,
						page: this.page,
						pageSize: this.pageSize,
						searchTerm: vm.searchTerm,
						orderBy: vm.orderBy,
						style: vm.houseStyleId
					},
					type: "post",
					dataType: "json",
					async: true,
					success: function(data) {
						vm.products = data;
						vm.loading = false;
						if (data.totalRecords == 0) {
							vm.noProduct = true;
						}
					}
				});
				cancelLoading();
			},
			init: function() {
				this.brandId = null;
				this.priceId = null;
				this.page = 1;
				this.updateProducts();
			}
		},
		watch: {
			brandId: function(newV, oldV) {
				this.updateProducts();
			},
			priceId: function(newV, oldV) {
				this.updateProducts();
			},
			products: function() {
				init();
			},
			page: function() {
				this.updateProducts();
			},
			searchTerm: function() {
				this.updateProducts();
			},
			orderBy: function() {
				this.updateProducts();
			},
			houseTypeId: function() { // 户型变了，去重新获取品牌
			},
			houseStyleId: function() { // 风格变了，去重新获取品牌
				getRecBrands();
			}
		},
		created: function() {
			//console.log('created...')
			getProducts();
			getBrands();
			getRecBrands();
			getHouseTypes();
			getHouseStyles();
			getSpaces();
			//$('.xx-box').hide();
		} 
	});	
	
//});
	function addProduct(obj){
		console.log('click')
		var ele = $(obj).closest('li');
		var name = ele.find('.l1 > h2').text();
		var price = parseInt(ele.find('.l4 > p').text().substring(1));
		var img = ele.find('.img img').attr('src');
		var specificationId = $(obj).attr('data-id');
		var r = vm.rooms[vm.roomIndex];
		var sb = r.items[vm.sbIndex];
		sb.img = img;
		sb.count = 1;
		sb.price = price;
		sb.specificationId = specificationId;
		sb.name = name;
		//$(this).removeClass('pro_btn');
		//$(this).addClass('pro_btn_selected');
		//event.preventDefault();
		/*
		vm.changeItem({
			name:name,
			price:price,
			id:0,
			img:img,
			specificationId: specificationId
		}); */
	}
function init() {
	/*
	$(window).scroll(function(){
		if(vm.isScolling){
			return ;
		}
		var scollTop = $(document).scrollTop();
		var index = 0;
		$('.left_pz dl').children().each(function(_index){
			var offset = $(this).offset().top;
			if(scollTop > (offset-20)){
				if(_index > index){
					index = _index;
				}
			}
		});
		vm.changeIndex(index);
	}); */
//});	
}

function getSpaces() {
	$.ajax({
		url: "/design/getSpaces",
		dataType: "json",
		success: function(data) {
			vm.rooms = data;
		}
	});
}

function getProducts() {
	return {data:[]};
}
function getBrands() {
	$.ajax({
		url: "/design/getBrands",
		dataType: "json",
		success: function(data) {
			vm.brands = data;
		}
	});
}

function getHouseTypes() {
	$.ajax({
		url: "/design/getHouseTypes",
		dataType: "json",
		success: function(data) {
			vm.houseTypes = data;
		}
	});
}
function getHouseStyles() {
	$.ajax({
		url: "/design/getHouseStyles",
		dataType: "json",
		success: function(data) {
			vm.houseStyles = data;
		}
	});
}

function getRecBrands() {
	var houseStyleId;
	if (vm) {
		houseStyleId = vm.houseStyleId;
	}
	$.ajax({
		url: "/design/getRecBrands",
		dataType: "json",
		data: {
			houseStyleId: houseStyleId
		},
		success: function(data) {
			vm.recBrands = data;
		}
	});
}

function save(publish) {
	var specs = '';
	for (var i = 0; i < vm.rooms.length; i++) {
		var r = vm.rooms[i];
		for (var j = 0; j < r.items.length; j++) {
			var item = r.items[j];
			if (!item.specificationId) {
				continue;
			}
			if (item.count < 1) {
				continue;
			}
			specs += (item.specificationId + '-' + item.count + "-" + item.id + ",");
		}
	}
	if (specs == '') {
		error('请选择产品');
		return;
	}
	
	var communityId = $('#communityId').val();
	var houseTypeId = vm.houseStyleId;
	var houseStyleId = vm.houseStyleId;
	var houseStyleImage = $('#houseStyleImage').val();
	var name = $('#design-name').val();
	var area = $('#area').val();
	if (name == '') {
		error("请输入方案名称");
		return;
	}
	if (area == '') {
		error("请输入面积");
		return;
	}
	
	var logined;
	$.ajax({
		url: "/isLogined",
		dataType: "json",
		async: false,
		success: function(data) {
			if (data.success) {
				logined = true;
			} else {
				logined = false;
			}
		}
	});
	if (!logined) {
		
		layer.open({
		    type: 2,
		    title: '请先登陆',
		    skin: 'layui-layer-rim', //加上边框
		    area: ['480px', '320px'], //宽高
		    content: "/common_login"
		    
		});
		return;
	}
	
	$.ajax({
		url: "/design/saveDesign",
		type: "post",
		data: {
			communityId: communityId,
			houseTypeId: houseTypeId,
			houseStyleId: houseStyleId,
			houseStyleImage: houseStyleImage,
			name: name,
			area: area,
			// specs: specs, //不传这种拼起来的形式了，而是传json过去
			json: $.toJSON(vm.rooms),
			publish: publish
		},
		dataType: "json",
		success: function(data) {
			if (data.success) {
				ok("保存成功");
			} else {
				error(data.msg);
			}
		}
	});
}